package com.dev.utils;


import com.dev.receiver.WifiEventSubscriber;
import ohos.aafwk.ability.AbilitySlice;
import ohos.event.commonevent.CommonEventManager;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.MatchingSkills;
import ohos.rpc.RemoteException;
import ohos.wifi.WifiEvents;

/**
 * wifi状态监听工具类
 */
public class WifiUtils {

    /**
     * 初始化wifi监听过滤器
     *
     */
    public static WifiEventSubscriber initMatch(AbilitySlice slice) {
        MatchingSkills match = new MatchingSkills();
        match.addEvent(WifiEvents.EVENT_ACTIVE_STATE);
        CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(match);
        subscribeInfo.setPriority(100);
        WifiEventSubscriber subscriber = new WifiEventSubscriber(subscribeInfo, slice);
        return subscriber;
    }

    /**
     * 订阅wifi改变事件
     *
     * @param subscriber
     */
    public static void subscribeWifi(WifiEventSubscriber subscriber) {
        try {
            CommonEventManager.subscribeCommonEvent(subscriber);
        } catch (RemoteException e) {
            LogUtils.warn("WIFI", "subscribe in wifi events failed!");
        }
    }

    /**
     * 取消订阅wifi改变事件
     */
    public static void unSubscribeWifi(WifiEventSubscriber subscriber) {
        try {
            CommonEventManager.unsubscribeCommonEvent(subscriber);
        } catch (RemoteException e) {
            LogUtils.warn("WIFI", "unsubscribe in wifi events failed!");
        }
    }

}
